//
//  MyHomeViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "MyHomeViewController.h"
#import "MyHomeView.h"
#import "MyHomeTableViewCell.h" // 自定义cell
#import "UserInfoTableViewController.h" // 用户信息
#import "SettingsTableViewController.h"
#import "MyCollectionTableViewController.h"
#import "UserInfoModel.h"
#import "LoginViewController.h"
#import "SystemMessagesViewController.h"



@interface MyHomeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) MyHomeView *myHomeView ;

@end

@implementation MyHomeViewController

#pragma mark - 初始化视图 未加载完成的时候调用
- (void)loadView
{
    self.myHomeView = [[MyHomeView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.view = self.myHomeView;
}

#pragma mark - 初始化 视图加载完成时调用
- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置触发事件和代理
    [self setViewEventAndDelegate];
    
    // 设置导航栏
    [self loadNavigationIndfo];
    
    // 监听广播
    [self notificationEvent];
    
}

#pragma mark - 监听各类广播
- (void)notificationEvent
{
    // 监听登录成功的广播
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:kLoginSuccessed object:nil];
    
    // 监听退出登录的广播
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:kexitSuccessed object:nil];
    
    // 监听修改用户成功的广播
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:kUpdateUserInfoSuccessed object:nil];
}

#pragma mark - 登陆成功后刷新表格
- (void)refresh
{
    [self.myHomeView.tableView reloadData];
    
    // 移除通知
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 设置视图的事件和代理
- (void)setViewEventAndDelegate
{
    
    self.myHomeView.tableView.delegate = self;
    self.myHomeView.tableView.dataSource = self;
    
    // 设置按钮触发事件
    [self.myHomeView.settingsButton addTarget:self action:@selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 设置按钮触发事件
- (void)settingsButtonAction
{
    SettingsTableViewController *settingsVc = [[SettingsTableViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:settingsVc];
    nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 设置导航栏
- (void)loadNavigationIndfo
{
    
    self.navigationItem.title = @"我的家园";
    
    // 标题颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor RGBColor:71 green:128 blue:208 alpha:1];
}

#pragma mark - 设置分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark -- 设置每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    else
    {
        return 2;
    }
}

#pragma mark -- 自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0)
    {
        static NSString *myHomeCellIdentifier = @"myHomeCell";
        [tableView registerClass:[MyHomeTableViewCell class] forCellReuseIdentifier:myHomeCellIdentifier];
        MyHomeTableViewCell *myHomeCell = [tableView dequeueReusableCellWithIdentifier:myHomeCellIdentifier];
        
        // 判断登陆状态
        // 先取出登陆状态
        NSString *loginStatus = [[NSUserDefaults standardUserDefaults] stringForKey:kLoginStatus];
        if ([loginStatus isEqualToString:@"YES"])
        {
            // 从数据库取出数据
            NSMutableArray *userInfoArray = [FMDBManage selectAllUserInfo];
            UserInfoModel *userInfoModel = [userInfoArray objectAtIndex:0];
            myHomeCell.avatarImageView.image = userInfoModel.figureurl_qq_2; // 头像
            myHomeCell.nameLabel.text = userInfoModel.nickname; // 用户名
        }
        else
        {
            myHomeCell.avatarImageView.image = [UIImage imageNamed:@"avatar"]; // 头像
            myHomeCell.nameLabel.text = @"点击登陆"; // 用户名
        }

        return myHomeCell;
    }
    else
    {
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        switch (indexPath.row)
        {
            case 0:
                cell.textLabel.text = @"我的收藏";
                break;
            case 1:
                cell.textLabel.text = @"系统消息";
                break;
            default:
                break;
        }
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

#pragma mark - 点击cell触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0)
    {
        // 判断登陆状态
        // 先取出登陆状态
        NSString *loginStatus = [[NSUserDefaults standardUserDefaults] stringForKey:kLoginStatus];
        if ([loginStatus isEqualToString:@"YES"])
        {
            // 调转到用户信息界面
            UserInfoTableViewController *userInfoVc = [[UserInfoTableViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:userInfoVc];
            nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
            // 从数据库取出数据
            NSMutableArray *userInfoArray = [FMDBManage selectAllUserInfo];
            
            // 属性传值
            userInfoVc.userInfoModel = [userInfoArray objectAtIndex:0];
            
            [self presentViewController:nav animated:YES completion:nil];
        }
        else
        {
            // 跳转到登陆页面
            LoginViewController *loginViewVc = [[LoginViewController alloc] init];
//            [self.navigationController pushViewController:loginStatus animated:YES];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginViewVc];
            nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            [self presentViewController:nav animated:YES completion:nil];
        }

    }
    else
    {
        if (indexPath.row == 0) {
            // 判断登陆状态
            // 先取出登陆状态
            NSString *loginStatus = [[NSUserDefaults standardUserDefaults] stringForKey:kLoginStatus];
            if ([loginStatus isEqualToString:@"YES"])
            {
                MyCollectionTableViewController *myCollection = [[MyCollectionTableViewController alloc]init];
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:myCollection];
                [self presentViewController:nav animated:YES completion:nil];
            }
            else // 跳转到登录
            {
                // 跳转到登陆页面
                LoginViewController *loginViewVc = [[LoginViewController alloc] init];
                //            [self.navigationController pushViewController:loginStatus animated:YES];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginViewVc];
                nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                [self presentViewController:nav animated:YES completion:nil];
            }
        } else if (indexPath.row == 1)
        {
            SystemMessagesViewController *systemVc = [[SystemMessagesViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:systemVc];
            [self presentViewController:nav animated:YES completion:nil];
        }
    }
}

#pragma mark - 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 100;
    }
    return 44;
}


#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}
@end
